# encoding:utf-8

import requests
import base64

def identify_id(Image_location):
    '''
    数字识别
    '''

    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/numbers"
    # 二进制方式打开图片文件
    f = open(Image_location, 'rb')
    img = base64.b64encode(f.read())

    params = {"image":img}
    access_token = '24.99abc56881638c948ae2eb8997b9b875.2592000.1638954044.282335-25126089'
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        try:
            # print(response.json())
            # 获取电动车牌号
            number=response.json()['words_result'][0]['words']
            print('识别的电动车牌号为：',number)
            if len(number) != 7:
                number = '请查看图片'
            # print(type(number))
        except Exception as e:
            # error = response.json()
            print(e)
            print('识别失败')
            number = '请查看图片'
        finally:
         return number
if __name__ == '__main__':
    # dir = r'D:\101-Study\PycharmProjects\test\yolov5-5.0\1.jpg'
    # n = identify_id(dir)
    # print(n)
    # print(len(n))
    # if len(n) != 7:
    #     n = "请查看图片"
    n = "请查看图片"
    n = len(n)
    print(n)


